package com.probiz.estoresa.sandbox.web.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.core.controller.BaseController;
import com.probiz.estoresa.sandbox.model.ValidationTestModel;

public class ValidationTestController extends BaseController {

	public ModelAndView save(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ValidationTestModel entity = new ValidationTestModel();
		ServletRequestDataBinder binder = bindAndValidate(request, entity, "validationTestModel"); //validationTestModel bindName用于spring:bind path
		BindException errors = new BindException(binder.getBindingResult());
		// 传给子类的实现，后者可以继续验证和加入错误到errors
		if (errors.hasErrors()) {// 里面如果出错应该抛出异常
			logger.error("~~~~error~~~~");
		}
		Map model = errors.getModel();
		ModelAndView mav = getModelAndView("sandbox/validationTest", model);
		return mav;
	}
	
	public ModelAndView save2(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView("sandbox/validationTest2");
		return mav;
	}
	
	public ModelAndView defaultAction(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView("sandbox/validationTest");
		mav.addObject("validationTestModel", new ValidationTestModel());
		return mav;
	}
}
